
import {
  _decorator, Component, TiledMap, RigidBody2D, ERigidBody2DType,
  BoxCollider2D, Vec2, Node, macro
} from 'cc';
import { Hero } from './hero'
const { ccclass, property } = _decorator;

enum GameState {
  // 初始化（Init）：显示游戏菜单，初始化一些资源。
  GS_INIT,
  // 游戏进行中（Playing）：隐藏游戏菜单，玩家可以操作角色进行游戏。
  GS_PLAYING,
  // 结束（End）：游戏结束，显示结束菜单。
  GS_END,
};
@ccclass('Game')
export class Game extends Component {
  // [1]
  // private _curState: GameState = GameState.GS_INIT;

  // dummy = '';
  @property({ type: TiledMap })
  public tiledMap: TiledMap | null = null;
  @property({ type: Hero })
  public playerCtrl: Hero | null = null;
  @property({ type: Node })
  public startMenu: Node | null = null;
  @property({ type: Node })
  public endMenu: Node | null = null;
  @property({ type: Node })
  public optionsMenu: Node | null = null;
  // [2]
  // @property({ type: GameState })
  public curState: GameState = GameState.GS_INIT;

  start() {
    macro.ENABLE_TILEDMAP_CULLING = false;
    this.curState = GameState.GS_INIT;
    this.updateGameState(this.curState)
  }
  private updateGameState(value: GameState) {
    switch (value) {
      case GameState.GS_INIT:
        this.init();
        break;
      case GameState.GS_PLAYING:
        this.startMenu.active = false;
        setTimeout(() => {      //直接设置active会直接开始监听鼠标事件，做了一下延迟处理
          this.playerCtrl.setInputActive(true);
        }, 0.1);
        break;
      case GameState.GS_END:
        break;
    }
  }
  init() {
    this.startMenu.active = true;
    this.playerCtrl.setInputActive(false);
  }

  onStartButtonClicked() {
    this.curState = GameState.GS_PLAYING;
    this.updateGameState(this.curState)
  }
  /**
   * @description: TODO: 设置墙体碰撞，未生效，待查
   * @param {*}
   * @return {*}
   */
  setWallCollider() {
    if (this.tiledMap) {
      const tiledSize = this.tiledMap.getTileSize()
      const layer = this.tiledMap.getLayer('wall')
      const layerSize = layer.getLayerSize()

      const { width, height } = layerSize
      for (let i = 0; i < width; i++) {
        for (let j = 0; j < height; j++) {
          const tield = layer.getTiledTileAt(i, j, true)
          if (tield.uuid) {
            const body = tield.node.addComponent(RigidBody2D)
            body.group = 2
            body.type = ERigidBody2DType.Static
            const collider = tield.node.addComponent(BoxCollider2D)
            collider.group = 2
            collider.offset = new Vec2(tiledSize.width / 2, tiledSize.height / 2)
            collider.size = tiledSize
            collider.apply()
          }
        }
      }
    }
  }
  // update (deltaTime: number) {
  //     // [4]
  // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
